package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.cardview.CardPagerAdapter;
import com.runen.infinitemovement.cardview.ShadowTransformer;
import com.runen.infinitemovement.model.entity.ADDataEntity;
import com.runen.infinitemovement.model.entity.SportsStopEntity;
import com.runen.infinitemovement.tool.ADToWebOrActivityUtil;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

import java.util.List;

public class MedalPoppupWindow extends PopupWindow {
    private Context context;
    private View layout;
    private ImageView iv_dismiss;
    private TextView tv_name, tv_see_details;
    private RoundedCornersImage rci_bg;
    private ADDataEntity dataBeanList;

    public OnHozonClickListener getOnHozonClickListener() {
        return onHozonClickListener;
    }


    public void setOnHozonClickListener(OnHozonClickListener onHozonClickListener) {
        this.onHozonClickListener = onHozonClickListener;
    }

    private OnHozonClickListener onHozonClickListener;

    public static MedalPoppupWindow getInstance(Context context, ADDataEntity dataBeanList) {
        return new MedalPoppupWindow(context, dataBeanList);
    }

    private MedalPoppupWindow(Context context, ADDataEntity dataBeanList) {
        super(context);
        this.context = context;
        this.dataBeanList = dataBeanList;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_medal, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_name = layout.findViewById(R.id.tv_name);
        tv_see_details = layout.findViewById(R.id.tv_see_details);
        rci_bg = layout.findViewById(R.id.rci_bg);
        if (dataBeanList != null) {
            GlideUtils.loadImage(context, dataBeanList.cover, rci_bg);
            tv_name.setText(dataBeanList.positionName);
            if (dataBeanList.positionName.contains("红包")) {
                tv_see_details.setText("立即领取");
            } else {
                tv_see_details.setText("查看详情");
            }
        }
    }


    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_see_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBeanList != null) {
                    ADToWebOrActivityUtil.toType(context, dataBeanList);
                    dismiss();
                }
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnHozonClickListener {
        void onHozomClick(int number);
    }
}
